﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Odds.Entity;
using Odds.DBUtility;
using System.Data;
using System.Data.SqlClient;

namespace Odds.DataAccess
{
    public class OddsMatchTypeController
    {
        private const string SQL_GETALLUNDELETEDODDSMATCHTYPE = "SELECT MatchTypeID, MatchTypeName FROM OddsMatchType WHERE " + SQL_CONDITION_UNDELETED;
        private const string SQL_CONDITION_UNDELETED = " IsDeleted = 0 ";
        public Dictionary<string, Guid> Insert(List<OddsMatchType> oddsMatchTypeSet)
        {
            Dictionary<string, Guid> result = new Dictionary<string, Guid>();
            try
            {
                StringBuilder sql = new StringBuilder();
                string matchTypeNameList = string.Join(@"',N'", oddsMatchTypeSet.Select(o => o.MatchTypeName).ToArray());

                foreach (OddsMatchType oddsMatchType in oddsMatchTypeSet)
                {
                    Guid guid = Guid.NewGuid();
                    sql.AppendFormat("EXEC Pro_DictionaryInsert 'OddsMatchType','MatchTypeID','MatchTypeName',N'{0}',N'{1}';",
                        guid, oddsMatchType.MatchTypeName);
                }
                sql.Append(@"SELECT MatchTypeName, MatchTypeID FROM OddsMatchType WHERE MatchTypeName IN (N'" +
                    matchTypeNameList + @"') AND " + SQL_CONDITION_UNDELETED);

                result = OddsSQLHelper.GetStringKeyDictionary(sql.ToString());
            }
            catch (Exception e)
            {
                throw e;
            }
            return result;
        }

        public Dictionary<string, Guid> GetMatchTypeDictionary()
        {
            Dictionary<string, Guid> result = new Dictionary<string, Guid>();
            SqlDataReader dr = OddsSQLHelper.ExecuteReader(CommandType.Text, SQL_GETALLUNDELETEDODDSMATCHTYPE, null);
            while (dr.Read())
            {
                result.Add(dr.GetString(1), dr.GetGuid(0));
            }
            return result;
        }
    }
}
